import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.sql.*;

public class settingsPage extends MainFrame
{
  JLabel title;
  
  JPasswordField oldPass;
  JPasswordField newPass;
  JPasswordField confirmPass;
  
  JButton btnChangePassword;


   public void Draw(){
       
       /* Labels */
       
       title = new JLabel("Change Password");
       title.setFont(new Font("Cambria", Font.PLAIN, 24));
       title.setForeground(Color.black);
       title.setBounds(10,215,300,50);
       MainFrame.panel.add(title);
       
       title = new JLabel("_____________________________________________________________");
       title.setFont(new Font("Cambria", Font.BOLD, 14));
       title.setForeground(Color.black);
       title.setBounds(0,220,400,50);
       MainFrame.panel.add(title);
       
       title = new JLabel("Enter Old Password: ");
       title.setFont(new Font("Cambria", Font.PLAIN, 14));
       title.setForeground(Color.black);
       title.setBounds(30,265,170,50);
       MainFrame.panel.add(title);
       
       title = new JLabel("Enter New Password: ");
       title.setFont(new Font("Cambria", Font.PLAIN, 14));
       title.setForeground(Color.black);
       title.setBounds(30,300,170,50);
       MainFrame.panel.add(title);
       
       title = new JLabel("Confirm New Password: ");
       title.setFont(new Font("Cambria", Font.PLAIN, 14));
       title.setForeground(Color.black);
       title.setBounds(30,335,250,50);
       MainFrame.panel.add(title);
       
        
       /* old Password input */
       oldPass = new JPasswordField(10);
       oldPass.setBounds(220,280,190,25);
       title.setFont(new Font("Cambria", Font.PLAIN, 14));
       MainFrame.panel.add(oldPass);
       
       /* new Password input */
       newPass = new JPasswordField(10);
       newPass.setBounds(220,315,190,25);
       title.setFont(new Font("Cambria", Font.PLAIN, 14));
       MainFrame.panel.add(newPass);
       
       /* confirm Password input */
       confirmPass = new JPasswordField(10);
       confirmPass.setBounds(220,350,190,25);
       title.setFont(new Font("Cambria", Font.PLAIN, 14));
       MainFrame.panel.add(confirmPass);
       
       /* Change Password Button */
       btnChangePassword = new JButton(new ImageIcon("changePassword.jpg"));
       btnChangePassword.setBounds(140,430,185,25);
       btnChangePassword.addActionListener(new ButtonListener());
       MainFrame.panel.add(btnChangePassword);


  }
  
   public class ButtonListener implements ActionListener{

     public void actionPerformed(ActionEvent e){      
       if(e.getSource() == btnChangePassword){
           try{
                        database.stmt = database.conn.createStatement();

                        database.rs = database.stmt.executeQuery("SELECT * FROM ACCOUNT");

                        while( database.rs.next() ){
                            if(newPass.getText().equals(confirmPass.getText())){
                                if(database.rs.getString("password").equals(oldPass.getText())){
                                        int numUpdated = 
                                        MainFrame.database.stmt.executeUpdate( "UPDATE ACCOUNT SET password = '" + confirmPass.getText() + "' WHERE idaccount = 1");
                                       JOptionPane.showMessageDialog(null,"Password changed successfully!");
                                       oldPass.setText("");
                                       newPass.setText("");
                                       confirmPass.setText("");
                                }
                                else{
                                    JOptionPane.showMessageDialog(null,"Wrong password. Please try again.");
                                }
                           }
                           else{
                                    JOptionPane.showMessageDialog(null,"Passwords do not match. Please try again.");
                           }
                          
                        }
                } catch( SQLException se ){}
        }
     }
     
   }
   }
